package com.example.demo.util;

import com.example.demo.model.common.CommonRequest;
import org.springframework.stereotype.Component;
import org.springframework.util.DigestUtils;

import java.util.Map;
import java.util.TreeMap;

@Component
public class SignUtils {
    public static String generateSign(CommonRequest.VerifyInfo verify) {
        // 创建排序后的参数Map
        TreeMap<String, String> params = new TreeMap<>();
        params.put("appId", verify.getAppId());
        params.put("password", verify.getPassword());
        params.put("timeStamp", verify.getTimeStamp());

        // 构建排序后的参数字符串
        StringBuilder content = new StringBuilder();
        for (Map.Entry<String, String> entry : params.entrySet()) {
            if (content.length() > 0) {
                content.append("&");
            }
            content.append(entry.getKey()).append("=").append(entry.getValue());
        }


        content.append("&access_key=").append(verify.getAccessKey());

        return DigestUtils.md5DigestAsHex(content.toString().getBytes()).toUpperCase();
    }
}